public class Solution81 {
    public boolean search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        boolean flag = false;
        while (l <= r) {
            int m = l + (r - l) / 2;
            if (nums[m] == target) {
                return true;
            }
            if (nums[l] == nums[m] && nums[m] == nums[r]) {
                if (flag) {
                    return false;
                } else {
                    return travers(nums, l, r, target);
                }
            }
            if (nums[l] <= nums[m]) {
                if (target >= nums[l] && target < nums[m]) {
                    r = m - 1;
                    flag = true;
                } else {
                    l = m + 1;
                }
            } else {
                if (target <= nums[r] && target > nums[m]) {
                    l = m + 1;
                    flag = true;
                } else {
                    r = m - 1;
                }
            }
        }
        return false;
    }

    private boolean travers(int[] nums, int l, int r, int target) {
        for (int i = l; i <= r; i++) {
            if (nums[i] == target) {
                return true;
            }
        }
        return false;
    }
}
